<?php

declare(strict_types=1);

namespace App\Controller\act;

use App\Controller\AbstractController;
use App\Service\act\ActService;
use App\Service\act\LuckyWheelService;
use App\Service\act\TaskService;
use App\Service\act\UserTaskService;
use App\Service\BaseService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use function Hyperf\Support\env;

#[Controller(prefix: "act.task")]
class TaskController extends AbstractController
{

    /**
     * 任务列表
     * @return null
     */
    #[RequestMapping(path: 'list')]
    public function taskList(){
        $params = $this->request->getQueryParams();
        try {
            $res = UserTaskService::getUserTaskForApi($params);
            return $this->ReturnJson->successFul(200, $res);
        } catch (\Throwable $e) {
            $this->logger->alert('TaskController.taskList.Exception：' . $e->getMessage());
            return $this->ReturnJson->failFul($e->getCode());
        }
    }

    /**
     * 幸运转盘任务
     * @return null
     */
    #[RequestMapping(path: 'luckywheeltasks')]
    public function luckyWheelTodayTask(){
        // 参数校验
        $params = $this->request->getQueryParams();
        $validator = $this->validatorFactory->make($params,
            [
                'uid' => 'required'
            ]
        );
        if ($validator->fails()) {
            return $this->ReturnJson->failFul(201, null, 1, $validator->errors()->first());
        }
        try {
            $res = LuckyWheelService::getUserLuckyWheelTodayTask((int)$params['uid']);
            return $this->ReturnJson->successFul(200, $res);
        } catch (\Throwable $e) {
            $this->logger->alert('TaskController.luckyWheelTodayTask.Exception：' . $e->getMessage());
            return $this->ReturnJson->failFul($e->getCode());
        }
    }

    /**
     * 奖项列表
     * @return null
     */
    #[RequestMapping(path: 'prizeobj/list')]
    public function prizeObjList(){
        // 参数校验
        $params = $this->request->getQueryParams();
        $validator = $this->validatorFactory->make($params,
            [
                'act_id' => 'required'
            ]
        );
        if ($validator->fails()) {
            return $this->ReturnJson->failFul(201, null, 1, $validator->errors()->first());
        }
        try {
            // 幸运转盘触发档次
            $pobjKey = !empty($params['trigger_level']) ? LuckyWheelService::getPobjLevelKeyByNumber((int)$params['trigger_level']) : '';

            $actId = is_numeric($params['act_id']) ? $params['act_id'] : explode(',', $params['act_id']);
            // 获取数据
            $list = TaskService::getPrizeObjList($actId, $pobjKey);
            $res = [];
            foreach ($list as $v) {
                $res[$v['act_id']][] = [
                    'pobj_id' => $v['pobj_id'],
                    'pobj_name' => $v['pobj_name'],
                    'pobj_key' => $v['pobj_key'],
                    'pobj_brief' => $v['pobj_brief'],
                    'pobj_icon' => $v['pobj_icon'],
                    'pobj_worth' => $v['pobj_worth'],
                    'prize_list' => $v['prize_list'],
                ];
            }
            unset($list);
            return $this->ReturnJson->successFul(200, $res);
        } catch (\Throwable $e) {
            $this->logger->alert('TaskController.prizeObjList.Exception：' . $e->getMessage());
            return $this->ReturnJson->failFul($e->getCode());
        }
    }

    /**
     * 用户抽奖次数
     * @return null
     */
    #[RequestMapping(path: 'user/drawnum')]
    public function userDrawNum(){
        // 参数校验
        $params = $this->request->getQueryParams();
        $validator = $this->validatorFactory->make($params,
            [
                'act_id' => 'required',
                'uid' => 'required',
            ]
        );
        if ($validator->fails()) {
            return $this->ReturnJson->failFul(201, null, 1, $validator->errors()->first());
        }
        try {
            $num = ActService::getUserDrawNum((int)$params['uid'], (int)$params['act_id']);
            return $this->ReturnJson->successFul(200, ['draw_num' => $num]);
        } catch (\Throwable $e) {
            $this->logger->alert('TaskController.userDrawNum.Exception：' . $e->getMessage());
            return $this->ReturnJson->failFul($e->getCode());
        }
    }

    /**
     * 抽奖
     * @return null
     */
    #[PostMapping(path: 'draw')]
    public function draw(){
        // 参数校验
        $params = $this->request->post();
        $params['uid'] = $this->request->getAttribute('uid');
        $validator = $this->validatorFactory->make($params,
            [
                'act_id' => 'required',
                'uid' => 'required',
            ]
        );
        if ($validator->fails()) {
            return $this->ReturnJson->failFul(201, null, 1, $validator->errors()->first());
        }
        try {
            // 获取数据
            $res = ActService::draw((int)$params['uid'], (int)$params['act_id']);
            return $this->ReturnJson->successFul(200, $res);
        } catch (\Throwable $e) {
            BaseService::writeExceptionMsg($e, __CLASS__.'.'.__FUNCTION__);
            return $this->ReturnJson->failFul($e->getCode());
        }
    }

    #[PostMapping(path: 'task/test')]
    public function teasTest(){
        // 参数校验
        $params = $this->request->post();
        $validator = $this->validatorFactory->make($params,
            [
                'uid' => 'required',
                'act_id' => 'required',
                'condition_type' => 'required',
                'condition_value' => 'required',
                'progress' => 'required',
            ]
        );
        if ($validator->fails()) {
            return $this->ReturnJson->failFul(201, null, 1, $validator->errors()->first());
        }
        try {
            // 获取数据
            $res = (new TaskService())->changeUserTaskProgress((int)$params['uid'], (int)$params['condition_type'], (int)$params['condition_value'], (int)$params['progress'], $params);
            if ($res) {
                return $this->ReturnJson->successFul(200);
            } else {
                return $this->ReturnJson->failFul();
            }
        } catch (\Throwable $e) {
            $this->logger->alert('TaskController.teasTest.Exception：' . $e->getMessage());
            return $this->ReturnJson->failFul($e->getCode());
        }
    }
}
